package LinkedList.反转链表;

import LinkedList.ListNode;

public class Test {

    //https://leetcode.cn/problems/reverse-linked-list/submissions/

    // 1. 迭代
    public ListNode reverseList1(ListNode head) {

        ListNode prev = null;
        ListNode cur = head;
        ListNode next = null;

        while(cur != null) {
            next = cur.next;
            cur.next = prev;
            prev = cur;
            cur = next;
        }

        return prev;
    }

    // 2. 反转 k 个，循环k - 1 次
    public ListNode reverseList2(ListNode head) {

        ListNode prve = new ListNode(-1);
        prve.next = head;

        ListNode temp;

        ListNode head2 = head;
        int count = 0;
        while(head2 != null) {
            count++;
            head2 = head2.next;
        }

        for(int i = 1; i < count; i++) {
            temp = head.next;
            head.next = temp.next;
            temp.next = prve.next;
            prve.next = temp;
        }

        return prve.next;
    }



}
